package project;

import java.io.*;
import java.net.URL;
import java.util.Vector;

/**
 *
 *
 * reference @author   jin
 *              Date: 2/3/09
 *              Connect to eBay website and read source
 *              HtmlSourceReaderImp
 * modified by jc218934
 */
public class HtmlSourceReaderToDoc
{
    boolean ignore = true;
    String itemStart= "<!-- SET01 -->  <tr";
    String itemEnd = "</tr><!-- /SET01 -->";
    String historyStart = "<span onmouseover=\"_tipon(this)\" onmouseout=\"_tipoff()\"><span class=\"google-src-text\" style=\"direction: ltr; text-align: left;\">";
    String historyEnd = "</span>";
    private Vector<String> itemList;
    private Vector<String> itemHistoryList;
    int itemIndex = -1;
    int historyIndex = -1;

    //Constructor
    public HtmlSourceReaderToDoc()
    {
        itemList = new Vector<String>();
        itemHistoryList = new Vector<String>();
    }

    //Task: Read given URL extract HTMl source code
    //Date: 2/3/09
    public Vector<String> readProducts(String url)
    {
        try
        {
            BufferedReader reader = getReader(url);

            String line = null;
            while( ( line = reader.readLine() ) != null )
            {
                if (line.contains(itemStart))
                {
                    ignore =false;
                    line = line.substring(line.indexOf(itemStart));
                    itemIndex++;
                }
                if (!ignore)
                {
                    String text = line;
                    if (line.contains(itemEnd))
                    {
                        line = line.substring(0, line.indexOf(itemEnd));
                        text += "\n";
                        ignore = true;
                    }
                    if (itemList.size() == itemIndex)
                        itemList.add(text);
                    else
                    {
                        itemList.set(itemIndex, itemList.get(itemIndex)+text);
                    }
                }

            }
            reader.close();
        }
        catch (Exception ex)
        {
        }
        return itemList;
    }

    public Vector<String> readHistory(String url)
    {
        url = "http://127.0.0.1/test/bidHistEnA2_files/translate_p.htm";
        try
        {
            BufferedReader reader = getReader(url);

            String line = null;
            while( ( line = reader.readLine() ) != null )
            {
                if (line.contains(historyStart))
                {
                    ignore =false;
                    line = line.substring(line.indexOf(historyStart));
                    historyIndex++;
                }
                if (!ignore)
                {
                    String text = line;
                    if (line.contains(historyEnd))
                    {
                        line = line.substring(0, line.indexOf(historyEnd));
                        text += "\n";
                        ignore = true;
                    }
                    if (itemHistoryList.size() == historyIndex)
                        itemHistoryList.add(text);
                    else
                    {
                        itemHistoryList.set(historyIndex, itemHistoryList.get(historyIndex)+text);
                    }
                }

            }
            reader.close();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return itemHistoryList;
    }

    //Connect to the URL server, open connection and get Reader (to Reader the source code)
    private static BufferedReader getReader(String url)
    {
        //Input String is recognized as URL
        if (url.startsWith("http://"))
        {
            try
            {
                return new BufferedReader(new InputStreamReader(new URL(url).openConnection().getInputStream()));
                //return new BufferedReader(new InputStreamReader(new YahooJapanTranslatedUrl(url).getUrl().openConnection().getInputStream()));

            }
            catch (Exception ex)
            {
                return null;
            }
        }
        else //Input String is not recognized as URL
        {
            return null;
        }
    }
}